package com.cloudy.s3.request;

import java.util.Date;

import com.cloudy.impl.AwsURLEncoder;


public class CopyObject extends S3RequestWithNoResponse {

	private static final String X_AMZ_COPY_SOURCE_IF_MODIFIED_SINCE = "x-amz-copy-source-if-modified-since";
	private static final String X_AMZ_COPY_SOURCE_IF_UNMODIFIED_SINCE = "x-amz-copy-source-if-unmodified-since";
	private static final String X_AMZ_COPY_SOURCE_IF_NONE_MATCH = "x-amz-copy-source-if-none-match";
	private static final String X_AMZ_COPY_SOURCE_IF_MATCH = "x-amz-copy-source-if-match";
	private static final String X_AMZ_COPY_SOURCE = "x-amz-copy-source";
	private static final String X_AMZ_METADATA_DIRECTIVE = "x-amz-metadata-directive";
	private static final String X_AMZ_META = "x-amz-meta-";
	private static final String REPLACE = "REPLACE";

	public CopyObject(String sourceBucket, String sourceObjectName, String destinationBucket, String destinationObjectName) {
		super(destinationBucket, "PUT");
		super.resource = destinationObjectName;
		String copySource = "/" + sourceBucket + "/"+sourceObjectName;
		String urlEncoded = AwsURLEncoder.urlEncode(copySource);
		super.addHeader(X_AMZ_COPY_SOURCE, urlEncoded);
	}
	
	public CopyObject copyMetadata(boolean copyMetadata){
		if(!copyMetadata){
			super.addHeader(X_AMZ_METADATA_DIRECTIVE, REPLACE);
		}
		return this;
	}
	
	public CopyObject copySourceIfMatch(String eTag){
		super.addHeader(X_AMZ_COPY_SOURCE_IF_MATCH, eTag);
		return this;
	}
	
	public CopyObject copySourceIfNoneMatch(String eTag){
		super.addHeader(X_AMZ_COPY_SOURCE_IF_NONE_MATCH, eTag);
		return this;
	}
	
	public CopyObject copySourceIfUnmodifiedSince(Date date){
		String sDate = createDateInAWSFormat(date);
		super.addHeader(X_AMZ_COPY_SOURCE_IF_UNMODIFIED_SINCE, sDate);
		return this;
	}	
	
	public CopyObject copySourceIfModifiedSince(Date date){
		String sDate = createDateInAWSFormat(date);
		super.addHeader(X_AMZ_COPY_SOURCE_IF_MODIFIED_SINCE, sDate);
		return this;
	}
	
	public CopyObject setUserMetaData(String name, String value) {
		super.addHeader(X_AMZ_META+name, value);
		return this;
	}
}
